package br.com.skytef.beans.WEB;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import br.com.skytef.dao.CardPedDao;
import br.com.skytef.entity.RelCardPedEntity;
import br.com.skytef.utils.Dates;


@ManagedBean(name = "RelatorioCardPedBean")
@ViewScoped
public class RelatorioCardPedBean {
	private List <RelCardPedEntity> rel;
	public List <RelCardPedEntity> getRel() {return rel;}
	public void setRel(List <RelCardPedEntity> rel) {this.rel = rel;}
	
	
	
	private List <RelCardPedEntity> relDetail;
	
	private RelCardPedEntity relatorio;
	public RelCardPedEntity getRelatorio() {return relatorio;}
	public void setRelatorio(RelCardPedEntity relatorio) {this.relatorio = relatorio;}

	
	private int consulta;
	public int getConsulta() {return consulta;}
	public void setConsulta(int consulta) {this.consulta = consulta;}
	
	private int idPedido;
	
	private Date dateIni;
	private Date dateFim;
	public Date getDateIni() {
		return dateIni;
	}
	public void setDateIni(Date dateIni) {
		this.dateIni = dateIni;
	}
	public Date getDateFim() {
		return dateFim;
	}
	public void setDateFim(Date dateFim) {
		this.dateFim = dateFim;
	}

	public RelatorioCardPedBean(){
		dateIni = new Date();
		dateFim = new Date();

	}
	

	private String msg;

	public void exibirRelatorio() throws Exception{
		FacesContext facesContext = FacesContext.getCurrentInstance();
	
		if (validar() == null){
			rel	= new CardPedDao().PedidosCartoes(consulta, Dates.dateUtilToSql(dateIni), Dates.dateUtilToSql(dateFim));
		}else{
			facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Aviso: " , msg));
		}
	}

	public String validar(){
		if(dateIni.equals("") && dateFim.equals("")){
			msg = "Os campos de data inicial e final devem ser preenchidos.";
			
		}if (consulta == 0){
			msg = "O campo de consulta deve preenchido.";

		}

		return msg;
	}
	
	public void selecionaDetail() throws Exception{
		relDetail = new CardPedDao().Pedidos(idPedido);
	}
	
	public void refresh() throws SQLException{
		relatorio = new RelCardPedEntity();
		setConsulta(0);
		setIdPedido(0);
		dateIni = new Date();
		dateFim = new Date();
		setRel(null);
		setRelDetail(null);
	}
	
	public int getIdPedido() {
		return idPedido;
	}
	public void setIdPedido(int idPedido) {
		this.idPedido = idPedido;
	}
	public List <RelCardPedEntity> getRelDetail() {
		return relDetail;
	}
	public void setRelDetail(List <RelCardPedEntity> relDetail) {
		this.relDetail = relDetail;
	}
	
	

}
